import {
  Controller,
  Get,
  HttpException,
  Param,
  Query,
  Redirect,
} from '@nestjs/common';
import { ShortLongMapService } from './short-long-map.service';

@Controller()
export class AppController {
  constructor(private readonly shortLongMapService: ShortLongMapService) {}

  @Get('short-url')
  getHello(@Query('url') url: string) {
    return this.shortLongMapService.generate(url);
  }

  @Get(':code')
  @Redirect()
  async getLongUrl(@Param('code') shortUrl: string) {
    const longUrl = await this.shortLongMapService.getLongUrl(shortUrl);
    if (!longUrl) throw new HttpException('链接不存在', 404);
    return {
      url: longUrl,
      statusCode: 302,
    };
  }
}
